package com.jbm.core.client;

import com.jbm.core.common.*;
import com.jbm.core.iface.IAcross;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Random;

/**
 * Created by root on 17-9-21.
 */
public class Client
{
/*
	private MainCallback m_maincallback;

	public MainCallback getM_maincallback()
	{
		return m_maincallback;
	}

	public void setM_maincallback(MainCallback m_maincallback)
	{
		this.m_maincallback = m_maincallback;
	}

	private int m_local_id;

	public int getM_local_id()
	{
		return m_local_id;
	}

	public void setM_local_id(int m_local_id)
	{
		this.m_local_id = m_local_id;
	}

	private String m_app_type;

	public String getM_app_type()
	{
		return m_app_type;
	}

	public void setM_app_type(String m_app_type)
	{
		this.m_app_type = m_app_type;
	}

	public short m_localport;

	public short getM_localport()
	{
		return m_localport;
	}

	public void setM_localport(short m_localport)
	{
		this.m_localport = m_localport;
	}

	private String m_temppath;

	public String getM_temppath()
	{
		return m_temppath;
	}

	public void setM_temppath(String m_temppath)
	{
		this.m_temppath = m_temppath;
	}

	private Object m_accmap_mutex = new Object();
	private HashMap<Integer,IAccount> m_accountmap;
	private Resource m_caqueue;

	private boolean m_cli_localport_init;
	private boolean m_cli_localid_init;
	private boolean m_cli_accmap_init;
	private boolean m_cli_ASCA_init;
	private boolean m_cli_ASCAConnect_init;

	public Client()
	{
		m_cli_localport_init = false;
		m_cli_localid_init = false;
		m_cli_accmap_init = false;
		m_cli_ASCA_init = false;
	}

	public void PushPoint(Integer account_id, ConnectAgent ca)
	{
		IAccount ia = null;

		synchronized (m_accmap_mutex)
		{
			ia = m_accountmap.get(account_id)
		}

		if (ia == null)
		{
			ia = new IAccount(account_id);

			synchronized (m_accmap_mutex)
			{
				m_accountmap.put(account_id,ia);
			}
		}
		ia.Push2Map(ca, m_maincallback);
	}

	public void OfflinePoint(Integer account_id, ConnectAgent ca)
	{
		IAccount ia = null;

		synchronized (m_accmap_mutex)
		{
			ia = m_accountmap.get(account_id);
		}

		if (ia != null)
		{
			ia.Offline2Map(ca, m_maincallback);
		}
	}

	public boolean Connect2Server(Integer account_id, String app_type)
	{
		ConnectAgent ca = GetCA(account_id, app_type);

		if (ca == null)
			return false;

		return ca.ConnectServer();
	}

	public ConnectAgent GetCA(int account_id, String app_type )
	{
		IAccount ia = null;

		synchronized (m_accmap_mutex)
		{
			ia = m_accountmap.get(account_id);
		}

		if (ia == null)
			return null;

		return ia.GetCA(app_type);
	}

	public boolean Connect(String serverhost,
	                    short serverport,
	                    String app_type,
	                    MainCallback callback)
	{
		m_app_type = app_type;
		m_maincallback = callback;

		if (!m_cli_localport_init)
		{
			Random r = new Random();
			m_localport = (short)(r.nextInt(8000) + 2000);
			m_cli_localport_init = true;
		}

		if (!m_cli_localid_init)
		{
			m_local_id = 0;
			m_cli_localid_init = true;
		}

		if (!m_cli_accmap_init)
		{
			m_accountmap = new HashMap<Integer, IAccount>();
			m_cli_accmap_init = true;
		}

		if (!m_cli_ASCA_init)
		{
			ConnectAgent ca = new ConnectAgent("app_server", serverhost, serverport, m_localport, MainServer.Server_type_Client, 5000, this);

			PushPoint(AccountCtrl.Account_Server_id, ca);

			m_cli_ASCA_init = true;
		}

		if (!m_cli_ASCAConnect_init)
			m_cli_ASCAConnect_init = Connect2Server(AccountCtrl.Account_Server_id, "app_server");

		return m_cli_ASCAConnect_init;
	}

	public IAcross Remote(Integer to_id, String app_type, IAcross content, String method, boolean hasresp, boolean waitresp, boolean excode, boolean intask, int start, int pagesize )
	{
		ConnectAgent ca = GetCA(to_id, app_type);

		if (ca == null)
			ca = GetCA(AccountCtrl.Account_Server_id, "app_server");

		return ca.Send(to_id, app_type, null, method, content, hasresp, waitresp, excode, intask, ConnectAgent.Send_Type_Any, null, start, pagesize );
	}

	public CSIAcross SendText(int receiver, String app_type, String msg)
	{
		IP2P p2p = new IP2P();

		p2p.MFromer = m_local_id;
		p2p.MReceiver = receiver;
		p2p.MMsg = msg;
		p2p.MInitiate = true;

		return Remote(receiver, app_type,
				p2p, "PushPoint", true, true, false, true);
	}
*/
}
